﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
    public partial class QuanLyCapBac : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Admin"] == null)
            {
                Response.Redirect("/quan-tri/login.html");
            }
            if(!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {
            rptCapBac.DataSource = BLL.Admin.DMCapBacHelper.List_DMCapBac();
            rptCapBac.DataBind();
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtCapBac.Text))
            {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên danh mục cấp bậc không được để trống!');", true);
                return;
            }
            BLL.Admin.DMCapBacHelper.Add_DanhMucCapBac(txtCapBac.Text);
            txtCapBac.Text = "";
            LoadData();
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCapBac.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Tên danh mục cấp bậc không được để trống!');", true);
                return;
            }
            BLL.Admin.DMCapBacHelper.Update_DMCapBac(int.Parse(lbCapBacId.Text),txtCapBac.Text);
            btSave.Visible = true;
            btUpdate.Visible = false;
            txtCapBac.Text = "";
            LoadData();
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            btSave.Visible = true;
            btUpdate.Visible = false;
            txtCapBac.Text = "";
        }

        protected void rptCapBac_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Edit")
            {
                int capbacid = int.Parse(e.CommandArgument.ToString());
                lbCapBacId.Text = capbacid.ToString();
                DAL.tbl_DM_CapBacCV capBacCv = BLL.Admin.DMCapBacHelper.Get_DMCapBac(capbacid);
                if(capBacCv !=null)
                {
                    txtCapBac.Text = capBacCv.Mota;
                    btSave.Visible = false;
                    btUpdate.Visible = true;
                }
            }
        }
    }
}